{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ce061973367468ad",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-09T16:53:54.617771Z",
     "start_time": "2025-07-09T16:53:46.811176300Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from gs_quant.instrument import EqOption\n",
    "from gs_quant.risk import EqAnnualImpliedVol, Price, EqSpot, EqDelta\n",
    "from gs_quant.session import GsSession, Environment\n",
    "from gs_quant.common import MeasureScenario\n",
    "from gs_quant.markets import PricingContext\n",
    "import datetime as dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8b13e419dcb97542",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-09T16:54:13.152886800Z",
     "start_time": "2025-07-09T16:54:04.718992900Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e9968f7e25e8115",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "MeasureScenario class allows us to override inputs such as spot or volatility when pricing an instrument. Here are some examples of how to use it with FX Options."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a647b948e0ccc30",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Initial FX Option\n",
    "eq_option = EqOption('.SPX')\n",
    "init_eq_option = eq_option.calc((EqSpot, EqDelta, Price))\n",
    "init_eq_option.to_frame()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "353a1c995d13edc9",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We can override inputs such as spot or volatility when pricing an instrument. Here are some examples of how to use it with Equities Options. We might be also interested in comparing delta calculated by the system and the approximation derived from the price change. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "952616a7c77ff7c2",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Override a single input (Spot) with MeasureScenario context manager\n",
    "with MeasureScenario(EqSpot, init_eq_option[EqSpot] * 1.01):\n",
    "    eq_risks_up = eq_option.calc((EqSpot, EqDelta, Price))\n",
    "eq_risks_up.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68bc3ab1301e268a",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with MeasureScenario(EqSpot, init_eq_option[EqSpot] * 0.99):\n",
    "    eq_risks_down = eq_option.calc((EqSpot, EqDelta, Price))\n",
    "eq_risks_down.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa5824ef58b1450d",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(init_eq_option[EqDelta] / 100)\n",
    "print(((eq_risks_up[Price] - eq_risks_down[Price]) / 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bec831f54e74bd45",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We can see that the delta values were close, which is expected."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6230da08bd5b48b9",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We can override multiple inputs this way."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5aebf58ca8fa787",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Override multiple inputs (Spot rate and Annual Implied Volatility) with MeasureScenario context manager and return multiple outputs\n",
    "with MeasureScenario(EqSpot, 6000):\n",
    "    with MeasureScenario(EqAnnualImpliedVol, 0.2):\n",
    "        eq_risks = eq_option.calc((Price, EqAnnualImpliedVol, EqSpot))\n",
    "eq_risks.to_frame()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85d3535611a8379",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We can also combine the measure override with other context managers, such as PricingContext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3fdad1581e368a05",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Set up time context for pricing, and override the input\n",
    "with PricingContext(dt.date(2025, 7, 5)):\n",
    "    with MeasureScenario(EqAnnualImpliedVol, 0.2):\n",
    "        eq_risks = eq_option.calc((Price, EqAnnualImpliedVol, EqSpot))\n",
    "eq_risks.result().to_frame()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
